#!/bin/sh

###############################################################################
#function: sync data from hadoop to specific search[1-5]
#input: src path  and dst path
#output: specific files that gunziped
#author: qss-test-team
#any questions contact ****@360.cn
#
#Notice: this shell do not think all return values so that clear and efficient
###############################################################################


source ~/synctool-test-team/MASTERDIR/scripts/syncdata.conf



#function: rsync data and gunzip
#input: source dir and dest dir and filename
#output: rsynced and gunziped file on dest dir
#notice: source dir must exist / mark in path at least 3, because the last filename based on that
function fsync {
		src=$1
		dst=$2
		file=$3
		gz=$4
		if [ -z  "$file" ] || [ -z "$src" ] || [ -z "$dst" ]
		then
			return
		fi

		sp=/
		dir_mark=$(echo $src | awk -F"$sp" '{print $NF}')
		if [ x$dir_mark != x"" ]
		then
			src=$src/
		fi

		nf=$(echo $src | awk -F"/" '{print NF}')
		new_gzfile=$(awk 'BEGIN{input="'$src'"; split(input,arr,"'$sp'"); printf "%s_%s_%s_%s_%s", arr["'$nf'" -4], arr["'$nf'" -3], arr["'$nf'" -2], arr["'$nf'"-1], "'$file'";}')
		new_file=$(echo $new_gzfile | sed 's/.gz//g')


		if [ 1 -eq $gz ]
		then
			rsync -arq --delete $src/$file $dst/$new_gzfile && gunzip -c $dst/$new_gzfile >> $dst/$new_file && rm -rf $dst/$new_gzfile
		else
			rsync -arq --delete $src/$file $dst/$new_file
		fi
		
}




mkdir -p $dst_dir
if [ 0 -ne $? ]
then
	echo "cannot mkdir $dst_dir and exit"
	exit
fi


local_host=$(hostname)
who=$(whoami)


	#files=$(find /da1/s/var/hdp/home/dm/data/wenda/search_to_engine/2013-06-2[0-9]|grep "txt")
	files=$($filelist_command)
	file_list=($files)


	len_file=${#file_list[@]}
	len_account=${#account_list[@]}
	len_host=${#host_list[@]}
	if [ 0 -eq $len_account ] || [ 0 -eq $len_host ]
	then
		printf "empty: %s\tor\t%s\tcheck pls!\n", account, host
		exit
	fi

	if [ 0 -eq $len_file ]
	then
		printf "Notice: input dir is empty: %s\n", $find_cmd
		exit
	fi


	idx=0
	while [ $idx -lt $len_file ]
	do
		for ((j=0;j<$len_host;++j))
		do
			host=${host_list[$j]}
				for ((k=0;k<$len_account;++k))
				do
					account=${account_list[$k]}
					if [ $account == $who ] && [ $host == $local_host ]
					then
						file=$(basename ${file_list[$idx]})
						src_dir=$(dirname ${file_list[$idx]})
						fsync $src_dir $dst_dir $file $is_gz
					fi
				((++idx))
				#eof account_list
				done
		#eof host_list
		done
	#eof file_list
	done
